package org.lab.indicator.basic;


import java.util.Date;
import java.util.List;

import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
import org.lab.data.MarketData;
import org.lab.data.Quote;
import org.lab.data.Ticker;
import org.lab.indicator.Indicator;
import org.lab.indicator.Parameter;

public class SMA extends Indicator{

	private Parameter period;
	private Ticker ticker;
	
	public SMA(String ticker) {
		this(ticker, 200);
	}

	public SMA(String ticker, int perArg) {
		period = new Parameter("Period", perArg);
		this.setParameter(period);
		this.ticker = MarketData.getTicker(ticker);
	}

	@Override
	public String getDescription() {
		return "Simple Moving Average";
	}

	@Override
	public String getName() {
		return "SMA";
	}
	
	public float getStandardDeviation() {
		return this.getStandardDeviation((Quote[]) ticker.getQuotes().toArray());
	}
	
	public float getStandardDeviation(Quote[] quotes) {
		DescriptiveStatistics stats = new DescriptiveStatistics();
 		
		for( int i = 0; i < quotes.length; i++) {
		        stats.addValue(((Quote)quotes[i]).getClose());
		}

		return  (float) stats.getStandardDeviation();		
	}
	
	public float getStandardDeviation(Quote quote, int period) {
		Quote[] array2 = new Quote[200];
		return  (float) getStandardDeviation((Quote[]) ticker.getQuotes(quote.getDate(), 200).toArray(array2));		
	}
	
	public float getStandardDeviation(Date startDate) {
		return 0;
	}

	public float getStandardDeviation(Date startDate, Date endDate) {
		if (startDate == null && endDate == null) {
			getStandardDeviation();
		} else if (endDate == null) {
			getStandardDeviation(startDate);
		}
		return 0;
	}
	
	public float getValue(Date date){
		// TODO Validations
		List<Quote> quotes = ticker.getQuotes(date, (Integer)period.getParam());
		float sum = 0;
		for (Quote quoteLoop:quotes) {
			sum += quoteLoop.getClose();
		}
		return sum/((Integer)period.getParam()).floatValue();
	}
}
